import requests
from django.shortcuts import render, redirect
from django.http import HttpRequest

from Buyer import utils
from Seller import models as seller_models
from Buyer import models

def index(request):
    goods_obj_list = seller_models.Goods.objects.all()
    return render(request, 'Buyer/index.html', locals())

def register(request):
    error_msg = ''
    username = request.POST.get('username')
    ret = models.Buyer.objects.filter(name=username)
    if ret:
        error_msg = '买家账号已存在'
    else:
        if request.method == 'POST':
            username = request.POST.get('username')
            userpass = request.POST.get('userpass')
            models.Buyer.objects.create(name=username, password=userpass)
            return redirect('/Buyer/login/')
    return render(request, 'Buyer/register.html', {'error_msg': error_msg})

def login(request):
    error_msg = {}
    if request.method == 'POST':
        username = request.POST.get('username')
        userpass = request.POST.get('userpass')
        if userpass is None:
            error_msg['password'] = '请输入密码'
        else:
            userpass = utils.pwd_encrypt(userpass)
            ret = models.Buyer.objects.filter(name=username)
            if ret:
                password = ret[0].password
                password = utils.pwd_encrypt(password)
                if userpass == password:
                    request.session['buyer_id'] = ret[0].id
                    request.session['username'] = username
                    goods_id = request.POST.get('goods_id')
                    if goods_id:
                        if request.session.get("record_views") == "car_jump":
                            del request.session['record_views']
                            return redirect('/Buyer/car_jump/?id=' + str(goods_id))
                        elif request.session.get("record_views") == "send_comment":
                            del request.session['record_views']
                            return redirect('/Buyer/send_comment/?id=' + str(goods_id))
                        elif request.session.get("record_views") == "goods_collection":
                            del request.session['record_views']
                            return redirect('/Buyer/goods_collection/?id=' + str(goods_id))
                    else:
                        return redirect('/Buyer/index/')
                else:
                    error_msg['password'] = '您输入的密码有误'
            else:
                error_msg['username'] = '账户不存在'
    return render(request, 'Buyer/login.html', {'error_msg': error_msg})

def logout(request):
    request.session.flush()
    return redirect('/Buyer/index')

def address_list(request):
    buyer_id = request.session.get('buyer_id')
    address_obj_list = models.Address.objects.filter(buyer_id=buyer_id)
    return render(request, 'Buyer/address_list.html', locals())

def address_add(request):
    if request.method == 'POST':
        name = request.POST.get('buyer_name')
        phone = request.POST.get('buyer_phone')
        address = request.POST.get('buyer_address')
        models.Address.object.create(
            receiver=name,
            phone=phone,
            address=address,
            buyer_id=request.session.get('buyer_id')
        )
        return redirect('/Buyer/address_list/')
    return render(request,'Buyer/address_add.html')

def address_delete(request):
    address_id=request.GET.get('id')
    models.Address.objects.filter(id=address_id).delete()
    return redirect('/Buyer/address_list/')

def address_change(request):
    if request.method == 'GET':
        address_id=request.GET.get('id')
        address_obj=models.Address.object.get(id=address_id)
        return render(request,'Buyer/address_change.html',{'address_obj':address_obj})
    else:
        address_id=request.POST.get('id')
        buyer_name=request.POST.get('buyer_name')
        buyer_phone=request.POST.get('buyer_phone')
        buyer_address=request.POST.get('buyer_address')
        models.Address.objects.filter(id=address_id).update(
            receiver=buyer_name,
            address=buyer_address,
            phone=buyer_phone,
        )
        return redirect('/Buyer/address_list/')

def my_collect_list(request):
    username=request.session.get("username")
    goods_collects=models.GoodsCollect.object.filter(buyer_name=username,collect_state=1)
    return render(request,'Buyer/my_collect_list.html',{'goods_collects':goods_collects})

def collect_cancel(request):
    goods_id=request.GET.get("goods_id")
    buyer_id=request.session.get("buyer_id")
    models.GoodsCollect.object.filter(goods_id=goods_id,buyer_id=buyer_id).update(collect_state=0)
    return redirect('/Buyer/my_collect_list/')

def history_collect_list(request):
    username=request.session.get("username")
    history_goods_collects=models.GoodsCollect.objects.filter(buyer_name=username,collect_state=0)
    return render(request,'Buyer/historu_collect_list.html',{'history_goods_collects':history_goods_collects})

def history_collect_anew(request):
    goods_id=request.GET.get("goods_id")
    buyer_id=request.session.get("buyer_id")
    models.GoodsCollect.object.filter(goods_id=goods_id,buyer_id=buyer_id).update(collect_state=1)
    return redirect('/Buyer/my_collect_list/')

def history_collect_delete(request):
    goods_id = request.GET.get("goods_id")
    buyer_id = request.session.get("buyer_id")
    models.GoodsCollect.object.filter(goods_id=goods_id, buyer_id=buyer_id).delete()
    return redirect('/Buyer/my_collect_list/')